<?php
//today_Headlines 今日头条文章列表抓取

include_once __DIR__ . '/../vendor/autoload.php';
include_once __DIR__ . '/../function.php';

use QL\QueryList;
use QL\Ext\Chrome;
use Illuminate\Database\Capsule\Manager as DB;

$db_table1 = 'product';
$db_table2 = 'articles';
$res = get_res($db_table1, $db_table2);

$ql = QueryList::getInstance();
// 注册插件，默认注册的方法名为: chrome
$ql->use(Chrome::class);

$ql->chrome(function ($page, $browser) use ($res, $db_table2, $db_table1) {
    $page->setUserAgent('Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36');

    foreach ($res as $value) {
        $insert_id = DB::table($db_table2)->insertGetId(array(
            'keyword' => $value['name'],
            'p_id' => 0,
            'source' => 0,
            'created_at' => date('Y-m-d H:i:s')
        ));

        $page->goto('https://www.toutiao.com/search/?keyword=' . $value['name']);
        $page->waitFor('.link.title');
        for ($i = 0; $i < 20; $i++) {
            $page->keyboard->press('Space');
            sleep(1);
        }
        // 获取页面HTML内容
        $html = $page->content();
        $range = '.sections .articleCard';
        $data = QueryList::html($html)->rules(array(
            'url' => array('.link.title', 'href'),
            'title' => array('.J_title', 'text')
        ))->range($range)->query()->getData();

        $data = $data->all();
        foreach ($data as &$v) {
            $url = 'https://www.toutiao.com' . $v['url'];
//            $page1->waitFor('.syl-page-article.syl-device-pc');
//            $html = $page1->content();
//            $v['content'] = getUrl($url);
//            var_dump($v['content']);
            $v['p_id'] = $insert_id;
            $v['url'] = 'https://www.toutiao.com' . $v['url'];
            $v['keyword'] = $value['name'];
            $v['source'] = 1;
            $v['created_at'] = date('Y-m-d H:i:s');
        }
        DB::table($db_table2)->insert($data);
        $page->close();
        break;
    }
    // 关闭浏览器
    $browser->close();
    // 返回值一定要是页面的HTML内容
    return $html;
}, [
    'headless' => false, // 启动可视化Chrome浏览器,方便调试
    'devtools' => false, // 打开浏览器的开发者工具
]);